using System;
using System.Web.UI;
using System.Web.UI.WebControls;

using Deepcode.Flux.Core.UI.Controls.Base;

namespace Deepcode.Flux.Core.UI.Controls
{
	/// <summary>
	/// AssetFileUploadValidator determines if a file has been selected
	/// in an AssetFileUpload control.
	/// </summary>
	public class AssetFileUploadValidator  : Deepcode.Flux.Core.UI.Controls.Base.BaseValidator
	{
		protected override void OnPreRender(EventArgs e)
		{
			base.OnPreRender (e);
			this.LoadClientScript("AssetFileUpload/afuv.js", "Deepcode.Flux.Core.UI.Controls.AssetFileUploadValidator");
		}

		protected override void AddAttributesToRender(HtmlTextWriter writer)
		{
			base.AddAttributesToRender (writer);
			if( RenderUplevel )
			{
				writer.AddAttribute("evaluationfunction", "AFU_Validate");
			}
		}

		protected override bool EvaluateIsValid()
		{
			try
			{
				// Take the text from the control
				string s = this.GetControlValidationValue( ControlToValidate );
				if( s==null || s == "" ) return false;
				return true;
			}
			catch
			{
				return false;
			}
		}
	}
}